package bb.lanxing.manager;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import java.io.Serializable;
import java.util.HashSet;

import bb.lanxing.App;
import bb.lanxing.util.ProcessUtil;
import bb.lanxing.util.TtsSettingHelper;

public class RemoteSharedPreference extends BaseSharedPreference implements ISPSharedPreference {
    private static final String TAG = "RemoteSharedPreference";

    public static final String PREFERENCE_FILE_NAME = "multi_process_sp";
    private final String ACTION_REMOTE_SP_CHANGED_BG;
    private final String ACTION_REMOTE_SP_CHANGED_MAIN;
    private final String EXTRA_REMOTE_SP_CHANGED_KEY;
    private final String EXTRA_REMOTE_SP_CHANGED_VALUE;
    private final String curProcessName;
    private static final String PROCESS_NAME_MAIN = App.getContext().getPackageName();
    private static final String PROCESS_NAME_BG = App.getContext().getPackageName() + ":bg";

    private static volatile RemoteSharedPreference instance;

    private final BroadcastReceiver broadcastReceiver;

    private final SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener;
    private final HashSet<ISPChangedListener> spChangedListenerSet;

    @Override
    public void commit() {
        super.commit();
    }

    @Override
    public boolean contains(String str) {
        return super.contains(str);
    }

    @Override
    public boolean getBoolean(String str, boolean z) {
        return super.getBoolean(str, z);
    }

    @Override
    public Enum getEnum(String str, Enum r2) {
        return super.getEnum(str, r2);
    }

    @Override
    public float getFloat(String str, float f) {
        return super.getFloat(str, f);
    }

    @Override
    public int getInt(String str, int i) {
        return super.getInt(str, i);
    }

    @Override
    public long getLong(String str, long j) {
        return super.getLong(str, j);
    }

    @Override
    public String getString(String str, String str2) {
        return super.getString(str, str2);
    }

    @Override
    public /* bridge */ /* synthetic
     */ void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        super.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
    }

    @Override
    public void remove(String str) {
        super.remove(str);
    }

    @Override
    public void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        super.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
    }

    @Override
    public boolean registerSPChangedListener(ISPChangedListener iSPChangedListener) {
        return this.spChangedListenerSet.add(iSPChangedListener);
    }

    @Override
    public boolean unregisterSPChangedListener(ISPChangedListener iSPChangedListener) {
        return this.spChangedListenerSet.remove(iSPChangedListener);
    }

    @SuppressLint("NewApi")
    public static RemoteSharedPreference getInstance() {
        synchronized (RemoteSharedPreference.class) {
            if (instance == null) {
                instance = new RemoteSharedPreference();
            }
        }
        return instance;
    }

    public static void terminate() {
        if (instance != null) {
            instance.release();
            instance = null;
        }
    }

    private RemoteSharedPreference() {
        super(App.getContext().getSharedPreferences(PREFERENCE_FILE_NAME, 0));
        ACTION_REMOTE_SP_CHANGED_MAIN = "bb.lanxing.action.REMOTE_SP_CHANGED_MAIN";
        ACTION_REMOTE_SP_CHANGED_BG = "bb.lanxing.action.REMOTE_SP_CHANGED_BG";
        EXTRA_REMOTE_SP_CHANGED_KEY = "EXTRA_REMOTE_SP_CHANGED_KEY";
        EXTRA_REMOTE_SP_CHANGED_VALUE = "EXTRA_REMOTE_SP_CHANGED_VALUE";
        spChangedListenerSet = new HashSet<>();
        onSharedPreferenceChangeListener = (sharedPreferences, str) -> {
            Object obj = sharedPreferences.getAll().get(str);
            for (ISPChangedListener ispChangedListener : spChangedListenerSet) {
                ispChangedListener.onParamChanged(str, obj);
            }
        };
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String stringExtra = intent.getStringExtra(EXTRA_REMOTE_SP_CHANGED_KEY);
                Serializable serializableExtra = intent.getSerializableExtra(EXTRA_REMOTE_SP_CHANGED_VALUE);
                Log.v(RemoteSharedPreference.TAG, "RemoteSharedPreference[" + curProcessName + "], onReceive, key = " + stringExtra + ", val = " + serializableExtra);
                setValue(stringExtra, serializableExtra, false);
            }
        };
        registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
        this.curProcessName = ProcessUtil.getProcessName();
        String str = PROCESS_NAME_BG;
        String sb = "RemoteSharedPreference, PROCESS_NAME_BG = " + str + ", curProcessName = " + this.curProcessName;
        Log.v(TAG, sb);
        if (str.equals(this.curProcessName)) {
            App.getContext().registerReceiver(broadcastReceiver, new IntentFilter(this.ACTION_REMOTE_SP_CHANGED_BG));
        } else if (PROCESS_NAME_MAIN.equals(this.curProcessName)) {
            App.getContext().registerReceiver(broadcastReceiver, new IntentFilter(this.ACTION_REMOTE_SP_CHANGED_MAIN), Context.RECEIVER_EXPORTED);
        }
    }

    @Override
    public void release() {
        unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
        super.release();
        try {
            App.getContext().unregisterReceiver(broadcastReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setHeartRateWarningValue(int i) {
        setValue(SPConstant.KEY_TTS_HEART_WARNING_VALUE, String.valueOf(i));
    }

    public int getHeartRateWarningValue() {
        if (SharedManager.getInstance().contains(SPConstant.KEY_TTS_HEART_WARNING)) {
            int i = SharedManager.getInstance().getInt(SPConstant.KEY_TTS_HEART_WARNING_VALUE, 0);
            SharedManager.getInstance().remove(SPConstant.KEY_TTS_HEART_WARNING);
            setValue(SPConstant.KEY_TTS_HEART_WARNING_VALUE, String.valueOf(i));
        }
        return Integer.parseInt(getString(SPConstant.KEY_TTS_HEART_WARNING_VALUE, "190"));
    }

    public void setPowerMax(int i) {
        setValue(SPConstant.KEY_POWER_MAX, i);
    }

    public int getPowerMax() {
        return getInt(SPConstant.KEY_POWER_MAX, 160);
    }

    public int getSpeedWarningValue() {
        return Integer.parseInt(getString(SPConstant.KEY_TTS_SPEED_WARNING_VALUE, "60"));
    }

    public float getInternetTemperature() {
        return getFloat(SPConstant.KEY_WEATHER_TEMPERATURE, 0);
    }

    public boolean getTTSSoundEnable() {
        return getBoolean(SPConstant.KEY_TTS_SOUND_ENABLE, false);
    }

    public int getTTSDuration() {
        return TtsSettingHelper.parseTTSDuration(getString(SPConstant.KEY_TTS_DURATION, "M5"));
    }

    public boolean getGPSSoundEnable() {
        return getInstance().getBoolean(SPConstant.KEY_GPS_SOUND_ENABLED, false);
    }

    public int getTTSDistance() {
        return TtsSettingHelper.parseTTSDistance(getString(SPConstant.KEY_TTS_DISTANCE, "KM1"));
    }

    public boolean isFloatPipEnable() {
        return getInstance().getBoolean(SPConstant.KEY_SPORT_FLOAT_PIP_ENABLE, false);
    }

    public int[] getSportTypes() {
        String string = getString(SPConstant.KEY_SPORT_DATA_TYPES, null);
        if (!TextUtils.isEmpty(string)) {
            String[] split = string.split(",");
            try {
                int[] iArr = new int[split.length];
                for (int i = 0; i < split.length; i++) {
                    iArr[i] = Integer.parseInt(split[i]);
                }
                return iArr;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public int getAltitudeSource() {
        if (contains(SPConstant.KEY_ALTITUDE_SOURCE)) {
            int i = getInt(SPConstant.KEY_ALTITUDE_SOURCE, 0);
            setValue(SPConstant.KEY_ALTITUDE_SOURCE_V2, String.valueOf(i));
            remove(SPConstant.KEY_ALTITUDE_SOURCE);
            return i;
        }
        String valueOf = String.valueOf(0);
        try {
            return Integer.parseInt(getString(SPConstant.KEY_ALTITUDE_SOURCE_V2, valueOf));
        } catch (Exception e) {
            e.printStackTrace();
            setValue(SPConstant.KEY_ALTITUDE_SOURCE_V2, valueOf);
            return 0;
        }
    }

    public int getAltitudeAdjustValue() {
        if (contains(SPConstant.KEY_ALTITUDE_ADJUST_VALUE)) {
            int i = getInt(SPConstant.KEY_ALTITUDE_ADJUST_VALUE, 0);
            setValue(SPConstant.KEY_ALTITUDE_ADJUST_VALUE_V2, String.valueOf(i));
            remove(SPConstant.KEY_ALTITUDE_ADJUST_VALUE);
            return i;
        }
        try {
            return Integer.parseInt(getString(SPConstant.KEY_ALTITUDE_ADJUST_VALUE_V2, "0"));
        } catch (Exception e) {
            e.printStackTrace();
            setValue(SPConstant.KEY_ALTITUDE_ADJUST_VALUE_V2, "0");
            return 0;
        }
    }

    @Override
    public void setValue(String str, Object obj) {
        setValue(str, obj, true);
    }

    public void setValue(String str, Object obj, boolean z) {
        super.setValue(str, obj);
        if (z) {
            notifyChange(str, obj);
        }
    }

    private void notifyChange(String str, Object obj) {
        Intent intent;
        if (PROCESS_NAME_BG.equals(this.curProcessName)) {
            intent = new Intent(this.ACTION_REMOTE_SP_CHANGED_MAIN);
        } else {
            intent = PROCESS_NAME_MAIN.equals(this.curProcessName) ? new Intent(this.ACTION_REMOTE_SP_CHANGED_BG) : null;
        }
        if (intent != null) {
            intent.putExtra(this.EXTRA_REMOTE_SP_CHANGED_KEY, str);
            intent.putExtra(this.EXTRA_REMOTE_SP_CHANGED_VALUE, (Serializable) obj);
            App.getContext().sendBroadcast(intent);
        }
    }
}